﻿using System.Windows;
using System.Windows.Controls;
using Autofac;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MG.Repository;
using MG.SmartHMI.Common;
using MG.SmartHMI.Domain;
using MG.SmartHMI.Entity.DAO;
using MG.SmartHMI.View;

namespace MG.SmartHMI.ViewModel
{
    public partial class LayoutViewModel : ObservableRecipient
    {
        [ObservableProperty]
        private string realDateTime;

        [ObservableProperty]
        private string headerTitle;

        [ObservableProperty]
        private UserControl frameViewSource;

        private System.Threading.Timer CircleTimer;

        public LayoutViewModel()
        {
            Register();
            SwitchView("homeview");
        }

        public void Register()
        {
            WeakReferenceMessenger.Default.Register<string, string>(this, "LayoutViewLoadSignal", LayoutViewLoadSignalSlot);
            WeakReferenceMessenger.Default.Register<string, string>(this, "LayoutViewUnLoadSignal", LayoutViewUnLoadSignalSlot);
        }

        private void LayoutViewLoadSignalSlot(object recipient, string message)
        {
            Task.Run(Init);
        }

        private void LayoutViewUnLoadSignalSlot(object recipient, string message)
        {
            CircleTimer?.Dispose();
            ModbusCtrl.Instance.Close();
        }

        [RelayCommand]
        private void SwitchView(string switchcode)
        {
            Application.Current.Dispatcher.BeginInvoke(() =>
            {
                switch (switchcode)
                {
                    case "homeview":
                        FrameViewSource = App.Service.Resolve<HomeView>();
                        break;

                    case "slnview":
                        //     if (!GlobalInfo.Default.CheckPermissions(ENUMROLE.Engineer | ENUMROLE.Admin)) return;
                        FrameViewSource = App.Service.Resolve<SlnView>();
                        break;

                    case "debugview":
                        FrameViewSource = App.Service.Resolve<DebugView>();
                        break;

                    case "settingview":
                        //if (!GlobalInfo.Default.CheckPermissions(ENUMROLE.Engineer | ENUMROLE.Admin)) return;
                        FrameViewSource = App.Service.Resolve<SettingView>();
                        break;
                }
            });
        }

        public void Init()
        {
            //这种定时器性能更高
            CircleTimer = new System.Threading.Timer(CircleTimeEvent, null, 3000, ModbusCtrl.Instance.TimeInterVal());
            var cfgdao = RepositoryService<Config>.Instance.Query(t => t.Key.Equals("AppTitle") && !t.IsDelete);
            if (cfgdao == null || cfgdao.Count() < 1)
            {
                RepositoryService<Config>.Instance.Add(new Config() { Key = "AppTitle", Value = "" });
                cfgdao = RepositoryService<Config>.Instance.Query(t => t.Key.Equals("AppTitle") && !t.IsDelete);
            }
            HeaderTitle = cfgdao.First().Value;
        }

        private void CircleTimeEvent(object state)
        {
            Task.Run(() =>
            {
                EventBus.Default.CircleTimeTrigger();
                RealDateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt");
            });
        }
    }
}